﻿using HalconDotNet;
using HWindow_Tool.Control;
using LD.Host;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace LD.Vision
{
    public class TemplateHelper
    {
        public static void CreateTemplate()
        {

        }
        public static OpResult FindTemplate(HWindow_Final hWindow, HImage img, TemplateModel template)
        {
            TemplateResultModel 结果;

            if (template == null)
            {
                Log4.Warr($"模板不能为空");
                return new OpResult() { IsSuccess = false };
            }
            结果 = null;
            bool isSuccess = false;
            HImage image = img.CopyImage();
            if (image == null)
                Log4.Warr($"无图片");

            template.hShapeModel.FindShapeModel(image,
                                        template.Param.StartRad,
                                        template.Param.EndRad,
                                        template.Param.MinScore,
                                        template.Param.NumMatches,
                                        template.Param.MaxOverlap,
                                        template.Param.SubPixel,
                                        template.Param.NumLevels,
                                        template.Param.Greediness,
                                        out var row,
                                        out var column,
                                        out var angle,
                                        out var score);
            if (row.Length > 0 && column.Length > 0 && angle.Length > 0 && score.Length > 0)
            {
                结果 = new TemplateResultModel()
                {
                    Angle = angle,
                    Column = column,
                    Row = row,
                    Score = score
                };

                //下面为roi区域的中心位置，非模板的中心位置
                HXLDCont xld1 = new HXLDCont();
                xld1.GenCrossContourXld(row, column, 60, new HTuple(0));
                hWindow?.DispObj(xld1, "green");

                for (int i = 0; i < score.Length; i++)
                {
                    Log4.Info($"模板匹配:row:{row},column:{column},angle:{angle},score:{score}");
                    //定义仿射变换
                    HHomMat2D hHomMat2D = new HHomMat2D();
                    hHomMat2D.HomMat2dIdentity();
                    hHomMat2D.VectorAngleToRigid(new HTuple(0), new HTuple(0), new HTuple(0), row[i], column[i], angle[i]);
                    //获取轮廓
                    var hxld = template.hShapeModel.GetShapeModelContours(1);
                    //反射变换
                    var contour = hHomMat2D.AffineTransContourXld(hxld);
                    //模板的中心位置
                    contour.AreaCenterPointsXld(out double row1, out var column1);

                    //匹配到的模板中心位置
                    //画x
                    //HXLDCont xld = new HXLDCont();
                    //xld.GenCrossContourXld(row1, column1, 60, new HTuple(45.0));
                    //hWindow.DispObj(xld);
                    //Log4.Info($"模板匹配:轮廓:row:{row1},column:{column1}");
                    hWindow?.DispObj(contour, "green");
                }

                isSuccess = true;
            }
            else
                Log4.Warr($"模板匹配:未找到");

            image.Dispose();
            return new OpResult() { IsSuccess = isSuccess, Append = 结果 };
        }
    }
}
